<template>
  <div>
    <h1>修改密码</h1>
    <form :model="payload" @submit.prevent="handleSubmit">
      <div>
        <label for="oldPassword">旧密码:</label>
        <input type="password" v-model="payload.oldPassword" required />
      </div>
      <div>
        <label for="newPassword">新密码:</label>
        <input type="password" v-model="payload.newPassword" required />
      </div>
      <button type="submit">提交</button>
    </form>
    <p v-if="responseMsg">{{ responseMsg }}</p>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { changePassword, type RootObject, type ChangePasswordPayload } from './profile'
const oldPassword = ref('')
const newPassword = ref('')
const responseMsg = ref<string | null>(null)
const payload: ChangePasswordPayload = {
  oldPassword: oldPassword.value,
  newPassword: newPassword.value
}
// 提交表单函数
const handleSubmit = async () => {
  console.log(payload)

  const response: RootObject = await changePassword(payload)
  //处理响应
  if (response.code === 200) {
    responseMsg.value = '密码修改成功！'
  } else {
      responseMsg.value = response.msg
      console.log(response) // 显示错误消息
  }
}
</script>

<style scoped></style>
